package com.jh.user.service;

import cn.jh.common.pojo.BaseResponse;
import cn.jh.common.utils.CommonConstants;
import cn.jh.common.utils.TokenUtil;
import com.jh.user.business.ShopRegisterBussiness;
import com.jh.user.business.StoreInfoBussiness;
import com.jh.user.pojo.ShopRegister;
import com.jh.user.pojo.StoreInfo;
import com.jh.user.util.CommonsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@EnableAutoConfiguration
public class StoreInfoService {

    @Autowired
    private StoreInfoBussiness storeInfoBussiness;
    @Autowired
    private ShopRegisterBussiness shopRegisterBussiness;

    /**
     * 商户增加门店
     */
    @RequestMapping(method = RequestMethod.POST, value = "/v1.0/user/store/add/{token}")
    public @ResponseBody
    Object addStoreRegister(HttpServletRequest request,
                            StoreInfo storeInfo,
                            @PathVariable("token") String token) {
        Object storeInfo1 = null;
        //校验参数
        if (storeInfo.equalsV(storeInfo)) {
            try {
                Long userId = TokenUtil.getUserId(token);
                if (userId == null) {
                    return BaseResponse.error(CommonConstants.ERROR_USER_NOT_EXISTED, "用户未登录");
                }

                String shopId = null == storeInfo.getShopId() ? "" : storeInfo.getShopId();
                if (!StringUtils.isEmpty(shopId)) {
                    ShopRegister shopRegister = shopRegisterBussiness.findByShopId(shopId);
                    if (null == shopRegister) {
                        return CommonsUtil.returnError("获取莎娜过户注册信息失败");
                    }
                    Long userId1 = shopRegister.getUserId();
                    storeInfo.setUserId(userId1);
                    storeInfo1 = storeInfoBussiness.mergeStoreInfo(storeInfo);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            return BaseResponse.error(CommonConstants.FALIED, "参数校验失败");
        }
        return storeInfo1;
    }


    /**
     * 分页查询 门店
     */
    @RequestMapping(method = RequestMethod.GET, value = "/v1.0/user/store/findVerify")
    public @ResponseBody
    Object findVerifyPage(HttpServletRequest request,
                          @RequestParam(value = "shopId", defaultValue = "", required = false) String shopId,
                          @RequestParam(value = "shopName", defaultValue = "", required = false) String shopName,
                          @RequestParam(value = "page", defaultValue = "0", required = false) int page,
                          @RequestParam(value = "size", defaultValue = "20", required = false) int size,
                          @RequestParam(value = "order", defaultValue = "DESC", required = false) Sort.Direction direction,
                          @RequestParam(value = "sort", defaultValue = "createTime", required = false) String sortProperty) {
        Pageable pageable = new PageRequest(page, size, new Sort(direction, sortProperty));
        return BaseResponse.success(storeInfoBussiness.findByShopIdAndLegalNameAndShopName(shopId, shopName, pageable));
    }


    /**
     * 查询门店编号
     */
    @RequestMapping(method = RequestMethod.GET, value = "/v1.0/user/store/findByShopId")
    public @ResponseBody
    Object findByShopId(HttpServletRequest request,
                        @RequestParam(value = "shopId", defaultValue = "", required = false) String shopId,
                        @RequestParam(value = "page", defaultValue = "0", required = false) int page,
                        @RequestParam(value = "size", defaultValue = "20", required = false) int size,
                        @RequestParam(value = "order", defaultValue = "DESC", required = false) Sort.Direction direction,
                        @RequestParam(value = "sort", defaultValue = "createTime", required = false) String sortProperty) {
        Pageable pageable = new PageRequest(page, size, new Sort(direction, sortProperty));
        return storeInfoBussiness.findsShopNanmeByShopId(shopId,pageable);
    }

    /**
     * 修改门店
     */
    @RequestMapping(method = RequestMethod.POST, value = "/v1.0/user/store/updataStoreInfo/{token}")
    public @ResponseBody
    Object updataStoreInfo(HttpServletRequest request, StoreInfo storeInfo,
                           @PathVariable("token") String token) throws Exception {
        try {
            Long userId = TokenUtil.getUserId(token);
            if (userId == null) {
                return BaseResponse.error(CommonConstants.ERROR_USER_NOT_EXISTED, "用户未登录");
            }
            storeInfoBussiness.updataStoreInfo(storeInfo);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseResponse.success();
    }
    /**
     * 删除门店
     */
    @RequestMapping(method = RequestMethod.POST, value = "/v1.0/user/store/deleteStoreInfo/{token}")
    public @ResponseBody
    Object deleteStoreInfo(HttpServletRequest request, StoreInfo storeInfo,
                           @PathVariable("token") String token) throws Exception {
        try {
            Long userId = TokenUtil.getUserId(token);
            if (userId == null) {
                return BaseResponse.error(CommonConstants.ERROR_USER_NOT_EXISTED, "用户未登录");
            }
            storeInfoBussiness.deleteStoreInfo(storeInfo);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseResponse.success();
    }
}